package com.leyou.item.service.impl;

import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.mapper.SpecGroupMapper;
import com.leyou.item.pojo.SpecGroup;
import com.leyou.item.service.SpecGroupService;
import com.leyou.common.utils.BeanHelper;
import com.leyou.item.service.SpecParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by IntelliJ IDEA.
 *
 * @author: wangzibeng
 * Date: 2019/5/4 0004
 * Time: 14:36
 */
@Service
public class SpecGroupServiceImpl implements SpecGroupService {
    @Autowired
    private SpecGroupMapper specGroupMapper;

    @Autowired
    private SpecParamService specParamService;

    @Override
    public List<SpecGroupDTO> querySpecGroupById(Long id) {
        SpecGroup specGroup = new SpecGroup();
        specGroup.setCid(id);
        List<SpecGroup> specGroupList = specGroupMapper.select(specGroup);
        if (specGroupList == null) {
            throw new LyException(ExceptionEnum.SPEC_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(specGroupList, SpecGroupDTO.class);
    }

    @Override
    public void editGroup(SpecGroupDTO specGroupDTO) {
        SpecGroup specGroup = BeanHelper.copyProperties(specGroupDTO, SpecGroup.class);
        int update = specGroupMapper.updateByPrimaryKeySelective(specGroup);
        if (update != 1) {
            throw new LyException(ExceptionEnum.UPDATE_OPERATION_FAIL);
        }
    }

    @Override
    public void deleteGroup(Long id) {
        int delete = specGroupMapper.deleteByPrimaryKey(id);
        if (delete != 1) {
            throw new LyException(ExceptionEnum.DELETE_OPERATION_FAIL);
        }
    }

    @Override
    public void addGroup(SpecGroupDTO specGroupDTO) {
        //先判断name是否为空（前端没做校验）
        if (specGroupDTO.getName() == null) {
            throw new LyException(ExceptionEnum.INVALID_PARAM_ERROR);
        }
        int insert = specGroupMapper.insertSelective(BeanHelper.copyProperties(specGroupDTO, SpecGroup.class));
        if (insert != 1) {
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
    }


    @Override
    public List<SpecGroupDTO> querySpecsByCid3(Long cid3) {
        // 查询规格组
        List<SpecGroupDTO> specGroupDTOS = querySpecGroupById(cid3);
        // 查询分类下所有的规格参数 根据cid
        List<SpecParamDTO> specParamDTOS = specParamService.querySpecParam(null, cid3, null);
        // 规格参数按组id进行分组
        Map<Long, List<SpecParamDTO>> paramMap = specParamDTOS
                .stream()
                .collect(Collectors.groupingBy(SpecParamDTO::getGroupId));
        //写到group中
        for (SpecGroupDTO specGroupDTO : specGroupDTOS) {
            specGroupDTO.setParams(paramMap.get(specGroupDTO.getId()));
        }
        return specGroupDTOS;
    }
}
